package net.huashitong.Indicators.service;

import com.alibaba.fastjson.JSONArray;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.Industry;
import net.huashitong.Indicators.utils.WorkbookUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 企业行业数据导入
 */
@Component
public class ImportDataProcess {

    private static final String COL_INDCODE = "行业编码";
    private static final String COL_INDNAME = "行业名称";
    private static final String COL_TITLE = "排行标题";
    private static final String COL_RANKS = "排名";
    private static final String COL_COMNAME = "单位名称";
    private static final String COL_ITEMNAME = "项目名称";
    private static final String COL_RANKDATA = "排名数据值";
    private static final String COL_DATATIME = "时间";
    private static final String COL_HAVEDATA = "是否有数值";

    private static final String [] lables = {"行业编码","行业名称","排行标题","排名","单位名称","项目名称","排名数据值","时间","是否有数值"};


    public List<Industry> importIndustry(String filePath) throws Exception{

        List<Industry> result = new ArrayList<Industry>();
        List<String> errors = new ArrayList<String>();

        Workbook book = WorkbookUtil.getWorkbook(filePath);
        Sheet sheet = book.getSheetAt(0);
        Iterator<Cell> it = sheet.getRow(0).cellIterator();
        int length =0;
        List<String> lableList = new ArrayList<String>();
        while(it.hasNext()){
            lableList.add(it.next().getStringCellValue());
            if(length > 9){
                throw new Exception("模板不正确，请先下载模板");
            }
            length++;
        }
        if(!arrayEquals(lableList.toArray(),lables)){
            throw new Exception("模板不正确，请先下载模板");
        }
        for (int rowIdx = 1; rowIdx <= sheet.getLastRowNum(); rowIdx++) {
            Row row = sheet.getRow(rowIdx);
            if (WorkbookUtil.isRowEmpty(row)) {
                continue;
            }
            try {
                Industry line = new Industry();

                //行业编码
                String industryCode = WorkbookUtil.getStringCellValue(row, COL_INDCODE);
                if(StringUtils.isBlank(industryCode)){
                    throw new Exception("行业编码不能为空");
                }else{
                    line.setIndustryCode(industryCode);
                }
                //行业名称
                String industryName = WorkbookUtil.getStringCellValue(row, COL_INDNAME);
                if(StringUtils.isBlank(industryName)){
                    throw new Exception("行业名称不能为空");
                }else{
                    line.setIndustryName(industryName);
                }
                //排行标题
                String title = WorkbookUtil.getStringCellValue(row, COL_TITLE);
                if(StringUtils.isBlank(title)){
                    throw new Exception("排行标题不能为空");
                }else{
                    line.setTitle(title);
                }
                //排名
                String ranks = WorkbookUtil.getStringCellValue(row, COL_RANKS);
                if(StringUtils.isBlank(ranks)){
                    throw new Exception("排名不能为空");
                }else{
                    line.setRanks(ranks);
                }
                //单位名称
                String companyName = WorkbookUtil.getStringCellValue(row, COL_COMNAME);
                if(StringUtils.isBlank(companyName)){
                    throw new Exception("单位名称不能为空");
                }else{
                    line.setCompanyName(companyName);
                }
                //项目名称
                String itemName = WorkbookUtil.getStringCellValue(row, COL_ITEMNAME);
                if(StringUtils.isNotBlank(itemName)){
                    line.setItemName(itemName);
                }
                //排名数据值
                String rankData = WorkbookUtil.getStringCellValue(row, COL_RANKDATA);
                if(StringUtils.isNotBlank(rankData)){
                    line.setRankData(rankData);
                }
                //时间
                String dataTime = WorkbookUtil.getStringCellValue(row, COL_DATATIME);
                if(StringUtils.isBlank(dataTime)){
                    throw new Exception("时间不能为空");
                }else{
                    line.setDataTime(dataTime);
                }
                //是否有数值
                String haveData = WorkbookUtil.getStringCellValue(row, COL_HAVEDATA);
                if(StringUtils.isBlank(haveData)){
                    throw new Exception("是否有数值不能为空");
                }else{
                    line.setHaveData(haveData);
                }
                result.add(line);
            } catch (Exception e) {
                errors.add("第" + rowIdx + "行：" + e.getMessage());
            }
        }
        if (errors.isEmpty() == false)
            throw new Exception(JSONArray.toJSONString(errors));
        return result;
    }


    /**
     * 判断两个数组是否相同
     * @param a
     * @param b
     * @return
     */
    public static boolean arrayEquals(Object[] a,Object[] b)
    {
        for (int i=0;i!=(a.length<b.length?a.length:b.length);i++)
            if (!a[i].equals(b[i])) return false;
        return true;
    }

}
